/*
* Copyright (C) 2024-2025 Deepin Co., Ltd.
*
* Author:      zhangbingyi <zhangbingyi@uniontech.com>
*
* Maintainer:  zhangbingyi <zhangbingyi@uniontech.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CONTENT_CONSISTANCE_WIDGET
#define CONTENT_CONSISTANCE_WIDGET

#include "widgets/safetyprotectionitem.h"
#include "widgets/defendertable.h"

#include <QLabel>
#include <QFrame>
#include <QPushButton>

#include <QWidget>
#include <QItemDelegate>

using namespace def::widgets;

class QTableView;
class TPTableModel;
class DBusInvokerInterface;
class TPTableDelegate;

class ProtectionSwitchItem : public SafetyProtectionItem
{
    Q_OBJECT
public:
    explicit ProtectionSwitchItem(QWidget *parent = nullptr)
        : SafetyProtectionItem(parent)
    {
    }

    virtual ~ProtectionSwitchItem() override {}

    virtual void resetUI() override;

    void syncIMAStatus(bool);

private:
    QLabel *m_tipLabelWithColor;
};

class TrustedProtectionWidget : public QFrame
{
    Q_OBJECT
public:
    explicit TrustedProtectionWidget(QWidget *parent = nullptr);
    virtual ~TrustedProtectionWidget() {}
    // 前端检查加入的文件类型
    bool verifyContentFileType(QString path);

Q_SIGNALS:
    // 文件添加
    // 删除规则
    // 启用与禁止
    // 请求打开-关闭功能
    void requestEnable(bool isEnable);

public Q_SLOTS:
    // 开关关闭，相关控件置灰
    void setWidgetDisabled(bool);
    void onSyncStatusEnabled(bool isEnable);

protected:
    void resizeEvent(QResizeEvent *event) override;
    void paintEvent(QPaintEvent *event) override;

private:
    void initUI();
    void initConnection();
    void initSwitch();
    void setFont();
    void initTable();
    void initModel();
    void updateCounterAfterInsert();
    Q_SLOT void updateCounter(QStandardItem *item);
    Q_SLOT void policyChanged(QStandardItem *item);
    Q_SLOT void selectAll(bool);
    Q_SLOT void addObject();
    Q_SLOT void removeObjects();
    void enableProtection(bool);
    void createEditor();
    Q_SLOT void createEditors(const QModelIndex &parent, int first, int last);
    Q_SLOT void syncStatus(bool);
    void updateCounterCheckBoxStatus();

    Q_SLOT void addObjectHelp(QAbstractButton *);
private Q_SLOTS:
    void rowAdded();
    int getCustomItemCount();

    void dataModelRowsRemoved(const QModelIndex &parent, int first, int last);
    void NoticeModelInitFinished();
private:
    // 固定控件
    // 主窗体
    QFrame *m_mainFrame;
    // 窗体左上标题
    QLabel *m_title;
    // 执行保护 + 开关
    ProtectionSwitchItem *m_switchItem;
    // 功能说明
    QLabel *m_describtion;
    // tableview,规则界面
    QTableView *m_tableView;
    // 用于格式化的视图
    DefenderTable *m_dfTableView;
    // 全选、反选
    QCheckBox *m_globalCheckBox;
    // 统计数据
    QLabel *m_counterLable;
    // 添加按钮
    QPushButton *m_addContentButton;
    QPushButton *m_removeContentButton;
    // 数据模型
    TPTableModel *m_dataModel;
    // 背景
//    DBackgroundGroup *m_bgGroup;

    bool m_isEnabled;
    QList<QStandardItem *> m_checkedItems;

    DBusInvokerInterface *m_IMAInvoker;
    DBusInvokerInterface *m_MonitorInvoker;
    bool m_isModelInited;
    TPTableDelegate *m_tableDelegate;
};
#endif
